package com.ykefu.assign.netty;

import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author likai
 * @date 2020/4/8 15:29
 * @email likai@7moor..com
 * @desc
 */
public class ChannelRelationStore {

    private static final Map<String,String> clientRelation = new ConcurrentHashMap();

    private static final Map<String, Set<String>> assignRelation = new ConcurrentHashMap<>();


    public static void store(String client,String assign){
        clientRelation.put(client,assign);
        Set<String> strings = assignRelation.get(assign);
        if(strings==null||strings.size()==0){
            strings = new HashSet<String>(){{add(client);}};
        }else {
            strings.add(client);
        }
        assignRelation.put(assign,strings);
    }

    /**
     *维护映射关系
     * @param client
     */
    public static void remove(String client){
        String assign = clientRelation.get(client);
        Set<String> strings = assignRelation.get(assign);
        strings.remove(client);
        clientRelation.remove(client);
    }


    /**
     * 维护映射关系
     * @param assign
     */
    public static void removeAssign(String assign){
        Set<String> strings = assignRelation.get(assign);
        strings.stream().forEach(key->
            clientRelation.remove(key));
    }


    public static Set<String> getClients(String assignId){
        return assignRelation.get(assignId);
    }

    public static String getAssign(String client){
        return clientRelation.get(client);
    }

}
